package net.huashitong.Indicators.web;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Indicators.entity.IndicatorBreakdown;
import net.huashitong.Indicators.filter.IndicatorBreakdownFilter;
import net.huashitong.Indicators.service.IndicatorBreakdownService;
import net.huashitong.Indicators.utils.ComparatorUtils;
import net.huashitong.Indicators.utils.DataUtils;
import net.huashitong.Indicators.utils.DateForIndUtils;
import net.huashitong.Indicators.utils.IndustryNameUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.ObjectUtils;
/**
 * 首页
 * @author lzr
 *
 */
@Controller
@RequestMapping("/app/indicatorBreakdown/")
public class IndicatorBreakdownAppController extends JsonBaseController {

	@Autowired
	private IndicatorBreakdownService indicatorBreakdownService;
	@Override
	protected String getView(String arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	@ResponseBody
	@RequestMapping("/queryAll")
	public Message queryAll(@RequestBody IndicatorBreakdownFilter filter){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		//计算未达标
		List<Map<String, Object>> list2 = new ArrayList<Map<String,Object>>();
		List<Map<String, Object>> list3 = new ArrayList<Map<String,Object>>();
		List<Map<String, Object>> list4 = new ArrayList<Map<String,Object>>();
		List<Map<String, Object>> list5 = new ArrayList<Map<String,Object>>();	
		String judge = DataUtils.getValue(filter);
		String datese = DataUtils.getValueMonth(judge,filter);
		datese = DateForIndUtils.getMonth(datese);
		/*String[] industryNames = {
				"农村居民人均可支配收入","地区生产总值","第一产业增加值","第二产业增加值","第三产业增加值","建筑业增加值","社会消费品零售总额","规模以上工业增加值","固定资产投资","工业固投","一般公共预算总收入","一般公共预算收入","出口总值","实际利用外资","农林牧渔业总产值","城镇居民人均可支配收入",
		};*/
		String[] industryNames ={
			"地区生产总值","公共财政总收入(不含基金预算收入)","农村居民人均可支配收入","农林牧渔业增加值","农林牧渔业总产值","出口总额","固定资产投资（不含农户）","地方公共财政总收入","城镇居民人均可支配收入","实际利用外资（验资口径）","居民消费价格总指数","建筑业总产值","社会消费品零售总额","第一产业","第二产业","第三产业","规模以上工业增加值","金融机构人民币贷款余额"	
		};
		for (int i = 0; i < industryNames.length; i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			List breakdown = indicatorBreakdownService.queryAll(datese,industryNames[i],filter.getArea());
			if(ObjectUtils.isBlank(breakdown)){
				continue;
			}
			Object[] object = (Object[]) breakdown.get(0);
			map.put("industryId", object[0]);
			Double double1 = Double.parseDouble(object[1].toString()); 
			Double double2 = Double.parseDouble(object[5].toString());
			BigDecimal data1 = new BigDecimal(double1); 
			BigDecimal data2 = new BigDecimal(double2); 
			int k = data1.compareTo(data2); 
			if(k<0){
				map.put("reach", "0");
			}else {
				map.put("reach", "1");
			}
			map.put("ratio", object[1]);
			map.put("industryName", object[2]);
			map.put("cumulative", object[3]);
			map.put("rank", object[4].toString());
			if("城镇居民人均可支配收入".equals(industryNames[i])||"农村居民人均可支配收入".equals(industryNames[i])){
				map.put("unit", "元");
			}else{
				map.put("unit", object[6]);
			}
			list.add(map);
		}
		
		Collections.sort(list, new ComparatorUtils()); 
		for (Map<String, Object> map : list) {
			//map = changeName(map);
			if("万元".equals(map.get("unit"))){
				String val =map.get("cumulative").toString();
				Double double1 = Double.parseDouble(val);
				double1 = double1/10000;
				String st= "";
				DecimalFormat df=new DecimalFormat("#.#");
				st=df.format(double1);
				map.put("cumulative", st);
			}else {
				String val =map.get("cumulative").toString();
				if(!"".equals(val)){
				Double double1 = Double.parseDouble(val);
				String st= "";
				DecimalFormat df=new DecimalFormat("#.#");
				st=df.format(double1);
				map.put("cumulative", st);
				}
			}
			Double double2 = Double.parseDouble(map.get("ratio").toString());
			DecimalFormat df=new DecimalFormat("#.#");
			String st=df.format(double2);
			map.put("ratio", st);
			if("0".equals(map.get("reach"))){
				list2.add(map);
			}
			if("1".equals(map.get("rank"))||"2".equals(map.get("rank"))||"3".equals(map.get("rank"))){
				list3.add(map);
			}
		}
		list4.add(list.get(0));
		list4.add(list.get(1));
		list4.add(list.get(2));
		String date = datese.substring(0, 4)+"年"+datese.substring(datese.length()-2)+"月";
		Map<String, Object> maps = new HashMap<String, Object>();
		maps.put("list", list3);
		maps.put("type", "1");
		maps.put("date", date);
		list5.add(maps);
		maps = new HashMap<String, Object>();
		maps.put("list", list4);
		maps.put("type", "2");
		maps.put("date", date);
		list5.add(maps);
		maps = new HashMap<String, Object>();
		maps.put("list", list2);
		maps.put("type", "3");
		maps.put("date", date);
		list5.add(maps);
		return coverMessage("200", "获取成功",list5);
	}
	
	
	public Map<String, Object> changeName(Map<String, Object> map){
		
		if(map.get("industryName").toString().contains("农村")){
			map.put("industryName", "农村人均收入");
		}
		if(map.get("industryName").toString().contains("公共预算总收入")){
			map.put("industryName", "公共预算总收入");
		}
		if(map.get("industryName").toString().contains("公共预算收入")){
			map.put("industryName", "公共预算收入");
		}
		if(map.get("industryName").toString().contains("第一")){
			map.put("industryName", "第一产业");
		}
		if(map.get("industryName").toString().contains("第二")){
			map.put("industryName", "第二产业");
		}
		if(map.get("industryName").toString().contains("第三")){
			map.put("industryName", "第三产业");
		}
		if(map.get("industryName").toString().contains("农林牧渔业增加值")){
			map.put("industryName", "农林牧渔业增加值");
		}
		if(map.get("industryName").toString().contains("农林牧渔业总产值")){
			map.put("industryName", "农林牧渔业总产值");
		}
		
		if(map.get("industryName").toString().contains("城镇")){
			map.put("industryName", "城镇人均收入");
		}
		if(map.get("industryName").toString().contains("规模以上工业增加值")){
			map.put("industryName", "规模以上工业");
		}
		if(map.get("industryName").toString().contains("建筑业")){
			map.put("industryName", "建筑业");
		}
		if(map.get("industryName").toString().contains("地区生产总值")){
			map.put("industryName", "GDP");
		}
		if(map.get("industryName").toString().contains("社会消费品零售总")){
			map.put("industryName", "社零总额");
		}
		if(map.get("industryName").toString().contains("实际利用")){
			map.put("industryName", "实际外资");
		}
		return map;
	}
	/**
	 * 排名页接口
	 * @param filter
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/queryRank")
	public Message queryRank(@RequestBody IndicatorBreakdownFilter filter){
		filter.setDate(DateForIndUtils.getMonth(filter.getDate()));
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		//String[] industryNames = {"地区生产总值","第一产业","第二产业","第三产业","建筑业增加值","社会消费品零售总额","规模以上工业增加值","固定资产投资","工业固投","一般公共预算总收入","一般公共预算收入","出口总值","实际利用外资"/*,"农林牧渔业总产值"*/,"城镇居民人均可支配收入","农村居民人均可支配收入","公共财政总收入(不含基金预算收入）"};
		//String[] industryNames={"地区生产总值","金融机构人民币贷款余额","社会消费品零售总额","建筑业总产值","实际利用外资","城镇居民人均可支配收入","公共财政总收入","地方公共财政总收入","固定资产投资","出口总额","农林牧渔业总产值","农林牧渔业增加值","农村居民人均可支配收入","规模以上工业增加值","第二产业","第三产业","第一产业"};
		String[] industryNames={"地区生产总值","农林牧渔业总产值","农林牧渔业增加值","规模以上工业增加值","固定资产投资","社会消费品零售总额","出口总额","实际利用外资","公共财政总收入","地方公共财政总收入","金融机构人民币贷款余额","城镇居民人均可支配收入","农村居民人均可支配收入","第二产业","第三产业","第一产业"};
		for (int i = 0; i < industryNames.length; i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			List breakdown = indicatorBreakdownService.queryAll(filter.getDate(),industryNames[i],filter.getArea());
			String mData="01";
			String siz="0";
			if(ObjectUtils.isBlank(breakdown)){
				if("1".equals(filter.getState())){
				    siz="1";
				    filter.setIndustryName(industryNames[i]);
					filter=IndustryNameUtils.changeId(filter);
					String  judge = DataUtils.getValue(filter);
					 String date = DataUtils.getValueMonth(judge,filter);
				    breakdown = indicatorBreakdownService.queryAll(DateForIndUtils.getMonth(date),industryNames[i],filter.getArea());
				    mData=date.substring(date.length()-2,date.length());
					}else{
						continue;
					}
			}
		
			Object[] object = (Object[]) breakdown.get(0);
			/*Double double1 = Double.parseDouble(object[1].toString());
			Double double2 =Double.parseDouble("0");*/
			
			/*if(ObjectUtils.isNotBlank(object[5])){
				 double2 = Double.parseDouble(object[5].toString());
			}
			
			BigDecimal data1 = new BigDecimal(double1); 
			BigDecimal data2 = new BigDecimal(double2); 
			int k = data1.compareTo(data2); 
			if(k<0){
				map.put("reach", "0");
			}else {
				if("1".equals(map.get("rank"))||"2".equals(map.get("rank"))||"3".equals(map.get("rank"))){
					map.put("reach", "2");
				}else {
					map.put("reach", "1");
				}
			}*/
			System.out.println("-------"+industryNames[i]);
			System.out.println("11--------"+object[0]);
			map.put("industryId", object[0]);
			map.put("ratio", object[1]);
			map.put("industryName", object[2]);
			map.put("cumulative", object[3]);
			map.put("rank", object[4]+"");
			if("城镇居民人均可支配收入".equals(industryNames[i])||"农村居民人均可支配收入".equals(industryNames[i])){
				map.put("unit", "元");
			}else{
				map.put("unit", object[6]);
			}
			
			list.add(map);
		}
		if(ObjectUtils.isBlank(list)){
			return coverMessage("200","暂无数据");
		}
		for (Map<String, Object> map : list) {
			//map = changeName(map);
			if("万元".equals(map.get("unit"))){
				Object val =map.get("cumulative");

				if(ObjectUtils.isBlank(val)){
					val="0";
				}
				Double double1 = Double.parseDouble(val.toString());
				double1 = double1/10000;
				String st= "";
				if(double1 < 1){
					map.put("cumulative", double1);
				}else {
					DecimalFormat df=new DecimalFormat("#.#");
					st=df.format(double1);
					map.put("cumulative", st);
				}
				map.put("unit", "亿元");
			}else{
				Object val =map.get("cumulative");
				if(ObjectUtils.isNotBlank(val)){
				Double double1 = Double.parseDouble(val.toString());
				String st= "";
				DecimalFormat df=new DecimalFormat("#.#");
				st=df.format(double1);
				map.put("cumulative", st);
				}else{
					map.put("cumulative", "0.0");
				}
			}
			if(!"-".equals(String.valueOf(map.get("ratio")))){
				Double double2;
			if(!"".equals(String.valueOf( map.get("ratio")) )){
				 double2 = Double.parseDouble(ObjectUtils.isBlank(map.get("ratio"))?"0":map.get("ratio").toString()  );
			}else{
				 double2 = Double.parseDouble("0");
			}
			
			if(double2>1){
				DecimalFormat df=new DecimalFormat("#.#");
				String st=df.format(double2);
				map.put("ratio", st);
			}else {
				DecimalFormat df=new DecimalFormat("#.#");
				String st=df.format(double2);
				map.put("ratio", st);
			}
			}
		}
		return coverMessage("200", "数据获取成功", list);
	}
	/**
	 * 返回有数据的月份
	 * @param filter
	 * @return
	 */
	@RequestMapping("/queryHaveValue")
	@ResponseBody
	public Message queryHaveValue(@RequestBody IndicatorBreakdownFilter filter){
		
		String judge = DataUtils.getValue(filter);
		Map<String, Object> map = new HashMap<String, Object>();
		String date = DataUtils.getValueMonth(judge,filter);
		map.put("judge", judge);
		map.put("date", date);
		return coverMessage("200", "获取成功", map);
	}
}
